# CMakeList.txt: MyCommon 的 CMake 项目，在此处包括源代码并定义
# 项目特定的逻辑。
#
cmake_minimum_required (VERSION 3.8)
project(MySharedLibDemo)

file(GLOB_RECURSE SHARED_INCLUDE ${PROJECT_SOURCE_DIR}/include/*.h ${PROJECT_SOURCE_DIR}/include/*.hpp)
file(GLOB_RECURSE SHARED_SRC ${PROJECT_SOURCE_DIR}/src/*.cpp ${PROJECT_SOURCE_DIR}/src/*.c)

option(IS_WINDOWS OFF)
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
	set(IS_WINDOWS ON)
endif()

# 添加三方库依赖
if(IS_WINDOWS)
    message(STATUS "Configuring on/for Windows")
	#message(FATAL_ERROR "Configuring on/for Windows")
	add_definitions(-D_WINDOWS)

# 寻找PCL依赖
	# 将PCL_ROOT环境变量中的"\"转为"/"，将转换后的值存入CMake变量
	file(TO_CMAKE_PATH $ENV{PCL_ROOT} PCL_ROOT) 
	# 将${PCL_ROOT}加入CMake的模块查找路径
	list(APPEND CMAKE_PREFIX_PATH ${PCL_ROOT}) 

	find_package(PCL REQUIRED)

	if (DEFINED PCL_INCLUDE_DIRS)
	message(INFO ${PCL_INCLUDE_DIRS})
	else()
	#set(PCL_INCLUDE_DIRS ${PCL_ROOT})
	message(WARNING ${PCL_INCLUDE_DIRS})
	endif()

	if(DEFINED PCL_LIBRARY_DIRS)
	message(INFO ${PCL_LIBRARY_DIRS})
	else()
	#set(PCL_LIBRARY_DIRS ${PCL_ROOT}/lib64-msvc-14.2)
	message(WARNING ${PCL_LIBRARY_DIRS})
	endif()

	file(GLOB PCL_LIBRARY_DIRS ${PCL_LIBRARY_DIRS}/*d.lib)

#添加Eigen头文件
	set(EIGEN_INCLUDE_DIRS $ENV{PCL_ROOT}/3rdParty/Eigen/eigen3)

# 寻找flann依赖 pcl的kdtree模块需要依赖这个库
	# Windows的pcl已经将flann集成到 3rdParty这个文件夹里了
	set(FLANN_INCLUDE_DIRS $ENV{PCL_ROOT}/3rdParty/FLANN/include)
	set(FLANN_LIBRARY_DIRS $ENV{PCL_ROOT}/3rdParty/FLANN/lib)

	file(GLOB FLANN_LIBRARIES ${FLANN_LIBRARY_DIRS}/*-gd.lib)

# 寻找vtk依赖 pcl的可视化模块需要依赖这个库
	# Windows的pcl已经将vtk集成到 3rdParty这个文件夹里了
	set(VTK_INCLUDE_DIRS $ENV{PCL_ROOT}/3rdParty/VTK/include)
	set(VTK_LIBRARY_DIRS $ENV{PCL_ROOT}/3rdParty/VTK/lib)

	file(GLOB VTK_LIBRARIES ${VTK_LIBRARY_DIRS}/*-gd.lib)

# 寻找opencv依赖
	# 将OPENCV_ROOT环境变量中的"\"转为"/"，将转换后的值存入CMake变量
	file(TO_CMAKE_PATH $ENV{OPENCV_ROOT} OPENCV_ROOT) 
	# 将${OPENCV_ROOT}加入CMake的模块查找路径
	list(APPEND CMAKE_PREFIX_PATH ${OPENCV_ROOT}) 

	find_package(Opencv REQUIRED)

	if (DEFINED OpenCV_INCLUDE_DIRS)
	message(INFO ${OpenCV_INCLUDE_DIRS})
	else()
	message(WARNING ${OpenCV_INCLUDE_DIRS})
	endif()

	if(DEFINED OpenCV_LIBRARY_DIRS)
	message(INFO ${OpenCV_LIBRARY_DIRS})
	else()
	message(WARNING ${OpenCV_LIBRARY_DIRS})
	endif()

# 寻找boost依赖
	# 将BOOST_ROOT环境变量中的"\"转为"/"，将转换后的值存入CMake变量
	file(TO_CMAKE_PATH $ENV{BOOST_ROOT} BOOST_ROOT) 
	# 将${BOOST_ROOT}加入CMake的模块查找路径
	list(APPEND CMAKE_PREFIX_PATH ${BOOST_ROOT}) 

	find_package(Boost REQUIRED)

	if (DEFINED BOOST_INCLUDE_DIRS)
	message(INFO ${BOOST_INCLUDE_DIRS})
	else()
	set(BOOST_INCLUDE_DIRS ${BOOST_ROOT})
	message(WARNING ${BOOST_INCLUDE_DIRS}/boost)
	endif()

	if(DEFINED BOOST_LIBRARY_DIRS)
	message(INFO ${BOOST_LIBRARY_DIRS})
	else()
	set(BOOST_LIBRARY_DIRS ${BOOST_ROOT}/lib64-msvc-14.2)
	message(WARNING ${BOOST_LIBRARY_DIRS})
	endif()

	file(GLOB BOOST_LIBRARIES ${BOOST_LIBRARY_DIRS}/*vc142-mt-gd-x64-1_74.lib)

else()
	# 假设是Linux
# 寻找PCL依赖
	find_package(PCL REQUIRED)
	
	if (DEFINED PCL_INCLUDE_DIRS)
	message(INFO ${PCL_INCLUDE_DIRS})
	else()
	#set(PCL_INCLUDE_DIRS "/usr/include/")
	message(WARNING "PCL_INCLUDE_DIRS has not been defined.")
	endif()

	if(DEFINED PCL_LIBRARY_DIRS)
	message(INFO ${PCL_LIBRARY_DIRS})
	else()
	#set(PCL_LIBRARY_DIRS "/usr/lib//x86_64-linux-gnu")
	message(WARNING ${PCL_LIBRARY_DIRS})
	endif()

	#file(GLOB BOOST_LIBRARIES ${PCL_LIBRARY_DIRS}/libboost*.so*)

# 寻找opencv以来
	find_package(OpenCV REQUIRED)

	if (DEFINED OpenCV_INCLUDE_DIRS)
	message(INFO ${OpenCV_INCLUDE_DIRS})
	else()
	#set(PCL_INCLUDE_DIRS ${PCL_ROOT})
	message(WARNING ${OpenCV_INCLUDE_DIRS})
	endif()

	if(DEFINED OpenCV_LIBRARY_DIRS)
	message(INFO ${OpenCV_LIBRARY_DIRS})
	else()
	#set(PCL_LIBRARY_DIRS ${PCL_ROOT}/lib64-msvc-14.2)
	message(WARNING ${OpenCV_LIBRARY_DIRS})
	endif()

# 寻找boost依赖
	find_package(Boost REQUIRED)
	
	if (DEFINED BOOST_INCLUDE_DIRS)
	message(INFO ${BOOST_INCLUDE_DIRS})
	else()
	set(BOOST_INCLUDE_DIRS "/usr/include/")
	message(WARNING ${BOOST_INCLUDE_DIRS}/boost)
	endif()

	if(DEFINED BOOST_LIBRARY_DIRS)
	message(INFO ${BOOST_LIBRARY_DIRS})
	else()
	set(BOOST_LIBRARY_DIRS "/usr/lib//x86_64-linux-gnu")
	message(WARNING ${BOOST_LIBRARY_DIRS})
	endif()

	file(GLOB BOOST_LIBRARIES ${BOOST_LIBRARY_DIRS}/libboost*.so*)
endif()

# 添加MYSDK依赖
file(TO_CMAKE_PATH $ENV{MYSDK_ROOT} MYSDK_ROOT) 
list(APPEND CMAKE_PREFIX_PATH ${MYSDK_ROOT}) 

set(MYSDK_INCLUDE_DIRS ${MYSDK_ROOT}/include)
set(MYSDK_LIBRARY_DIRS ${MYSDK_ROOT}/lib)

if (IS_WINDOWS)
	file(GLOB MYSDK_LIBRARIES ${MYSDK_LIBRARY_DIRS}/*.lib)
else()
	file(GLOB MYSDK_LIBRARIES ${MYSDK_LIBRARY_DIRS}/*.so)
endif()
set(SPDLOG_ROOT ${MYSDK_ROOT}/3rdParty/spdlog-1.9.2)

message(WARNING ${PROJECT_SOURCE_DIR}/include)
include_directories(
	${PROJECT_SOURCE_DIR}/include/

	${SPDLOG_ROOT}/include
	#${BOOST_INCLUDE_DIRS}
	${PCL_INCLUDE_DIRS}
	${FLANN_INCLUDE_DIRS}
	${VTK_INCLUDE_DIRS}

	${OpenCV_INCLUDE_DIRS}

	${BOOST_INCLUDE_DIRS}

	${MYSDK_INCLUDE_DIRS}
)


add_library(${PROJECT_NAME} SHARED)

target_link_directories(${PROJECT_NAME}
	PUBLIC
	${PROJECT_SOURCE_DIR}/include
	
)

target_sources(${PROJECT_NAME}
	PRIVATE
	${SHARED_INCLUDE}
	${SHARED_SRC}
	
)

message(WARNING ${PCL_LIBRARY_DIRS})
target_link_libraries(${PROJECT_NAME}
	PUBLIC
	#${BOOST_LIBRARIES}
	${PCL_LIBRARY_DIRS}
	${FLANN_LIBRARIES}
	${VTK_LIBRARIES}
	
	${OpenCV_LIBRARY_DIRS}

	${BOOST_LIBRARIES}

	${MYSDK_LIBRARIES}
)

#set(CMAKE_INSTALL_PREFIX ${WORKSPACE_PATH})
#message(FATAL_ERROR ${CMAKE_INSTALL_PREFIX})


# Windows下用Visual Studio得这么安装复制文件
if (IS_WINDOWS)
	add_definitions(-D_USRDLL -DSHARED_EXPORTS)

	# add_custom_command(
	# 	TARGET
	# 		${PROJECT_NAME}
	# 	POST_BUILD
	# 		COMMAND
	# 			${CMAKE_COMMAND} -E copy ${SHARED_INCLUDE} ${WORKSPACE_PATH}/include
	# 		COMMAND
	# 			${CMAKE_COMMAND} -E copy ${PROJECT_NAME}.dll ${WORKSPACE_PATH}/lib
	# 		COMMAND
	# 			${CMAKE_COMMAND} -E copy ${PROJECT_NAME}.lib ${WORKSPACE_PATH}/lib
	# 	VERBATIM
	# )

	if (DEFINED ENV{MYSDK_ROOT})
		# add_custom_command(
		# 	TARGET
		# 	${PROJECT_NAME}
		# POST_BUILD
		# 	COMMAND
		# 		${CMAKE_COMMAND} -E copy ${SHARED_INCLUDE} $ENV{MYSDK_ROOT}/include/Process
		# 	COMMAND
		# 		${CMAKE_COMMAND} -E copy ${PROJECT_NAME}.dll $ENV{MYSDK_ROOT}/bin
		# 	COMMAND
		# 		${CMAKE_COMMAND} -E copy ${PROJECT_NAME}.lib $ENV{MYSDK_ROOT}/lib
		# VERBATIM
		# )
	else()
		message(WARNING "ENV MYSDK_ROOT has not been defined!")
	endif()
endif()

# 在Linux上就可以这么安装
# if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
# 	file(COPY ${SHARED_INCLUDE}
# 		DESTINATION
# 		${WORKSPACE_PATH}/include 
# 	)
# 	file(COPY ${SHARED_INCLUDE}
# 		DESTINATION
# 		$ENV{MYSDK_ROOT}/include/Process
# 	)

# 	install(TARGETS ${PROJECT_NAME}
# 		LIBRARY DESTINATION
# 		${WORKSPACE_PATH}/lib
# 		)

# 		install(TARGETS ${PROJECT_NAME}
# 		LIBRARY DESTINATION
# 		$ENV{MYSDK_ROOT}/lib
# 	)
# endif()
